package com.longmao.demo.jdbc;

import com.longmao.model.LongEmp;

import java.sql.*;

/**
 * Created by zrb on 17/7/2.
 */
public class JdbcDemo {


    public static void main(String[] args)  {

        //注册驱动
        Connection conn =null;
        try {
            Class.forName("com.mysql.jdbc.Driver");


            //获得连接
            String url = "jdbc:mysql://127.0.0.1:3306/longmao";
            conn = DriverManager.getConnection(url, "root", "kpl123456");


            //构建sql执行对象
            String sql = "select * from long_emp";
            PreparedStatement pstmt = conn.prepareStatement(sql);


            //执行sql,获得执行结果
            ResultSet resultSet = pstmt.executeQuery();

            System.out.println("==========================");
            while (resultSet.next()) {

                Long id = resultSet.getLong("id");
                String empName = resultSet.getString("emp_name");

                //todo 日期待处理
                java.util.Date createTime = resultSet.getTimestamp("create_time");

                LongEmp longEmp = new LongEmp();
                longEmp.setId(id);
                longEmp.setEmpName(empName);
                longEmp.setCreateTime(createTime);


                System.out.println(longEmp);

            }

            System.out.println("==========================");

        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {

            try {
                conn.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
